<?php
	require_once('includes.php');
	Session::start();
	//This function is used in for signing up
	
	if (isset($_POST['submit'])) {
		$user_username = $_POST['username'];
		//Check if the user name is taken
		if (User::checkUsername($user_username)) {
			$error[] = 'ERROR_USERNAME_TAKEN';
		//Check the length of the username 
		} else if (strlen($user_username) < 4) {
			$error[] = 'ERROR_USERNAME_SHORT';
		//Check if the username is not valid
		} else if(preg_match('/[^a-zA-Z0-9_]/', $user_username) == 1) {
			$error[] = 'ERROR_USERNAME_INVALID';
		}
		
		$user_password = $_POST['password1'];
		//Check the length of the password
		if (strlen($user_password) < 8) {
			$error[] = 'ERROR_PASSWORD_SHORT';
		//Check if the password and the retype are the same
		} else if ($user_password !== $_POST['password2']) {
			$error[] = 'ERROR_PASSWORD_DIFFERENT';
		}		
		
		$user_email = $_POST['email'];
		//Check if the e-mail is input
		if(strlen($user_email) === 0){
			$error[] = 'ERROR_EMAIL_REQUIRED';
		//Check if the e-mail is taken
		} else if (User::checkEmail($user_email)) {
			$error[] = 'ERROR_EMAIL_TAKEN';
		} 
		
		$user_firstname = $_POST['firstname'];
		//Check if the first name is input
		if (strlen($user_firstname) === 0) {
			$error[] = 'ERROR_FIRSTNAME_REQUIRED';
		}
		
		//Check if the last name is input
		$user_lastname = $_POST['lastname'];
		if (strlen($user_lastname) === 0) {
			$error[] = 'ERROR_LASTNAME_REQUIRED';
		}
		$user_gender = $_POST['gender'];
		
		//Concat address of all the fields input
		$user_address = $_POST['address1'] . '|' . $_POST['address2'] . 
						'|' . $_POST['city'] . '|' . $_POST['state'] . '|' . $_POST['country'] . '|' . $_POST['postal'];
		$user_contact = $_POST['contact'];
		
		//Slash for American format (m/d/y), dashes/dots for European format (d-m-y)
		$user_dob = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year']; 
		$user_role = $_POST['role'];	
		$user_time = null;
		
		//User is doctor
		if ($user_role === 'doctor') {
			$doctor_regno = $_POST['regno'];
			if (strlen($doctor_regno) === 0) {
				$error[] = 'ERROR_REGNO_REQUIRED';
			}
		}
		
		//Check if there is error
		if (count($error) === 0) {
			//User is doctor
			if ($user_role === 'doctor') {
				$doctor_regno = $_POST['regno'];
				$doctor_speciality = $_POST['speciality'];
				$user = new Doctor($user_username, $user_password, $user_email, $user_firstname, $user_lastname, 
								$user_gender, $user_address, $user_contact, $user_dob, $user_role, $user_time,
								$doctor_regno, $doctor_speciality);
				$user_id = $user->insert();
				
				if ($user_id !== 0) {
					//Check if hospital is selected
					if (intval($_POST['hospital']) === 0) {
						//If not selected, check if new hospital is input
						$hospital_name = $_POST['hospitalName'];
						$hospital_address = $_POST['hospitalAddress'];
						$hospital_contact = $_POST['hospitalContact'];
						//Check if hospital name is empty
						if (strlen($hospital_name) !== 0) {
							//Check if hospital already exist
							if (Hospital::findID($hospital_name) !== 0) {
								$hospital_id = Hospital::findID($hospital_name);
							//If hospital not already exist
							} else {
								$hospital = new Hospital($hospital_name, $hospital_address, $hospital_contact);
								$hospital_id = $hospital->insert();
							}
						}
					} else {
						$hospital_id = $_POST['hospital'];
					}
					//Attach hospital to doctor
					$user->attachToHospital($hospital_id);
				}		
				header('Location: ' . DIR . '/complete.php?user=' . $user->getUsername() . '&token=' . $user->getValid());
			//User is patient
			} else {
				$patient_bloodtype = $_POST['bloodtype'];
				$patient_allergy = $_POST['allergy'];
						
				$user = new Patient($user_username, $user_password, $user_email, $user_firstname, $user_lastname, 
								$user_gender, $user_address, $user_contact, $user_dob, $user_role, $user_time,
								$patient_bloodtype, $patient_allergy);
				$user->insert();
				header('Location: ' . DIR . '/complete.php?user='. $user->getUsername() .'&token=' . $user->getValid());
			}
		//If there is an error
		} else {
			$_SESSION['message'] = $error;
			//Save the form
			$saved_form = array($user_username, $user_email, $user_firstname, $user_lastname, 
							$user_gender, $_POST['address1'], $_POST['address2'], $_POST['city'], $_POST['state'], 
							$_POST['country'], $_POST['postal'], $user_contact);
			$_SESSION['saved'] = $saved_form;
			header('Location: ' . DIR . '/signup.php?role=' . $user_role);
		}
	}
?>